package com.lc.cehui.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lc.cehui.R;

public class AmountView  extends LinearLayout implements View.OnClickListener, TextWatcher {
 
    private static final String TAG = "AmountView";
    private int amount = 1; //购买数量
    private int goods_storage = 99999; //商品库存
 
    private OnAmountChangeListener mListener;
 
    private TextView etAmount;
    private FrameLayout btnDecrease;
    private FrameLayout btnIncrease;
 
    public AmountView(Context context) {
        super(context);
    }
 
    public AmountView(Context context, AttributeSet attrs) {
        super(context, attrs);
 
        LayoutInflater.from(context).inflate(R.layout.number_add_sub_view, this);
        etAmount = (TextView) findViewById(R.id.etAmount);
        btnDecrease = (FrameLayout) findViewById(R.id.btnDecrease);
        btnIncrease = (FrameLayout) findViewById(R.id.btnIncrease);
 
        btnDecrease.setOnClickListener(this);
        btnIncrease.setOnClickListener(this);
        etAmount.addTextChangedListener(this);
    }
 
    public void setOnAmountChangeListener(OnAmountChangeListener onAmountChangeListener) {
        this.mListener = onAmountChangeListener;
    }
 
    public void setGoods_storage(int goods_storage) {
        this.goods_storage = goods_storage;
    }
 
    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.btnDecrease) {
            if (amount > 1) {
                amount--;
                etAmount.setText(amount + "");
            }
        } else if (i == R.id.btnIncrease) {
            if (amount < goods_storage) {
                amount++;
                etAmount.setText(amount + "");
            }
        }
        //清除焦点
        etAmount.clearFocus();
 
        if (mListener != null) {
            mListener.onAmountChange(this, amount);
        }
    }
 
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
 
    }
 
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
 
    }
 
    @Override
    public void afterTextChanged(Editable s) {
        if (s.toString().isEmpty())
            return;
        amount = Integer.valueOf(s.toString());
        if (amount > goods_storage) {
            etAmount.setText(goods_storage + "");
            return;
        }
 
        if (mListener != null) {
            mListener.onAmountChange(this, amount);
        }
    }
 
 
    public interface OnAmountChangeListener {
        void onAmountChange(View view, int amount);
    }

    public int getAmount() {
        return amount;
    }

    public void setAmount(int amount) {
        this.amount = amount;
        etAmount.setText(amount+"");
    }
}